#include "SceneNode.h"
//#include "main.h"
#include "WRenderer.h"

#include <memory>

SceneNode::SceneNode(std::string name)
{
	this->name = name;
	this->enabled = true;
}

SceneNode::~SceneNode()
{
	transform.release();
}


void SceneNode::AddChild(std::shared_ptr<SceneNode> child)
{
	children.push_back(child);
}

void SceneNode::AddMesh(std::shared_ptr<SceneMesh> mesh)
{
	meshes.push_back(mesh);
}

glm::mat4 SceneNode::GetModelMatrix()
{
	return transform->GetModelMatrix();
}

void SceneNode::SetModelMatrix(glm::mat4 matrix)
{
	transform->SetModelMatrix(matrix);
}

void SceneNode::Render(int pass)
{
	if(enabled)
	{
		for(int i=0; i<meshes.size(); i++)
		{
			meshes.at(i)->Render(pass);
		}

		for(int i=0; i<children.size(); i++)
		{
			children.at(i)->Render(pass);
		}
	}
}


void SceneNode::Update(std::shared_ptr<WRenderer> renderer)
{
	if(renderer != nullptr)
	{
		if(enabled)
		{
			for(int i=0; i<meshes.size(); i++)
			{
				unsigned int id = meshes.at(i)->GetDrawID();
				glm::mat4 modelMatrix = transform->GetModelMatrix();
				renderer->SetModelMatrix(id, modelMatrix);
			}
	
			for(int i=0; i<children.size(); i++)
			{
				children.at(i)->Update(renderer);
			}
		}
	}
}